package dao;

import java.util.ArrayList;
import java.util.List;
import dto.TitleDTO;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class TitleDAO extends DatabaseHelper {
	
    public static final String TABLE_TITLE = "Title";
    
    public static final String TITLE_ID = "ID";
    public static final String TITLE_CONTENT = "Content";
    
	public TitleDAO(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		
	}

    public void addTitle(TitleDTO titleDTO) 
    {
    	// Open database
        try {
	        ContentValues values = new ContentValues();
	        
	        values.put(TITLE_CONTENT, titleDTO.getContent());
	        
	        // Inserting rows
	        open();
	        DatabaseHelper.myDataBase.insert(TABLE_TITLE, null, values);
	
	        // Close database connection
	        DatabaseHelper.myDataBase.close();
        }
        catch (Exception ex) {}
    }         
    
    public TitleDTO getTitle(int id) {
        try {
	        Cursor cursor = DatabaseHelper.myDataBase.query(TABLE_TITLE, new String[] { TITLE_ID,
	        		TITLE_CONTENT}, TITLE_ID + "?",
	            new String[] {String.valueOf(id) }, null, null, null, null);
	        if (cursor != null)
	            cursor.moveToFirst();
	        
	        TitleDTO title = new TitleDTO(
	                Integer.parseInt(cursor.getString(0)),
	                cursor.getString(1));
	        return title;
        }
        catch (Exception e) {
        	return null;
        }
    }
        
    public List<TitleDTO> getAllTitles() {
    	try {
	        List<TitleDTO> titleList = new ArrayList<TitleDTO>();
	        
	        // Select all query
	        String selectQuery = "SELECT * FROM " + TABLE_TITLE;
	        open();
	        Cursor cursor = DatabaseHelper.myDataBase.rawQuery(selectQuery, null);
	        
	        // Looping through all rows and adding to list
	        
	        if (cursor.moveToFirst()) {
	            do {
	            	TitleDTO titleDTO = new TitleDTO(
	                		Integer.parseInt(cursor.getString(0)),
	                        cursor.getString(1));
	            
	            	titleList.add(titleDTO);
	            } while (cursor.moveToNext());
	        }
	        close();
	        return titleList;
    	}
    	catch (Exception e) {
			return null;
		}
    }
    
    public boolean updateTitle(TitleDTO titleDTO) {
        try {
	        ContentValues values = new ContentValues();
	        
	        values.put(TITLE_ID, titleDTO.getID());
	        values.put(TITLE_CONTENT, titleDTO.getContent());
	        open();
	        // Updating row
	        int isSucceed = DatabaseHelper.myDataBase.update(TABLE_TITLE, values, TITLE_ID + " = ?",
	                new String[] { String.valueOf(titleDTO.getID()) });
	        close();
	        if (isSucceed > 0) {
	        	return true;
	        }
	        	
	        return false;
        }
        catch (Exception ex) { 
        	return false; 
        }
    }
    
    public void deleteTitle(TitleDTO titleDTO) {
    	try {
    		DatabaseHelper.myDataBase.delete(TABLE_TITLE, TITLE_ID + " = ?",
                new String[] { String.valueOf(titleDTO.getID()) });
    		DatabaseHelper.myDataBase.close();
    	}
    	catch (Exception ex) {}
    }
    
    public void deleteAllTitles() 
    {
    	try {
    		DatabaseHelper.myDataBase.delete(TABLE_TITLE, null, null);
    	}
    	catch (Exception ex) {}
    }
    
    public int getTitleCount()
    {
    	try {
	        String countQuery = "SELECT * FROM " + TABLE_TITLE;
	        SQLiteDatabase db = this.getReadableDatabase();
	        Cursor cursor = db.rawQuery(countQuery, null);
	        cursor.close();
	        
	        // Return count
	        return cursor.getCount();
    	}
    	catch (Exception ex) {return -1;}
    }
}